// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.utils;

import java.util.List;

import org.dengues.commons.connectivity.IConnectivitySettingsConstants;
import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.process.ICompNode;
import org.dengues.core.property.EParameterFieldCategory;
import org.dengues.core.property.NodeParameterUtils;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-25 qiang.zhang $
 * 
 */
public class NodePropertyParameterUtils {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getValue".
     * 
     * @param node
     * @param name
     * @return
     */
    public static Object getValue(ICompNode node, String name) {
        return getValue(node, name, true);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getValue".
     * 
     * @param node
     * @param name
     * @param hasQuote
     * @return
     */
    public static Object getValue(ICompNode node, String name, boolean hasQuote) {
        List<NodePropertyParameter> params = node.getElementParameters();
        for (NodePropertyParameter param : params) {
            if (param.getName().equals(name)) {
                Object value = param.getValue();
                if (param.getFieldCategory().equals(EParameterFieldCategory.CHECK)) {
                    return value.toString();
                }
                if (param.getFieldCategory().equals(EParameterFieldCategory.WEB)) {
                    return NodeParameterUtils.delimitedParameterValue(value.toString(), true);
                }
                if (value instanceof String) {
                    if (!hasQuote) {
                        value = value.toString();
                    } else {
                        value = DenguesTextUtil.addQuotes(value.toString());
                    }
                }
                return value;
            }
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addQuotes".
     * 
     * @param connName
     * @param fieldName
     * @return
     */
    public static String addQuotes(String connName, String fieldName, boolean b) {
        if (connName == null) {
            return ""; //$NON-NLS-1$
        }
        int index = connName.indexOf(IConnectivitySettingsConstants.CONNECTIVITY_DELIMITER);
        if (index > 0) {
            String split = connName.substring(0, index).trim();
            index = split.indexOf("."); //$NON-NLS-1$
            if (index > 0) {
                String cat = split.substring(index + 1);
                if (cat.length() > 0) {
                    String dbType = cat.trim();
                    return DenguesTextUtil.addQuotesForJETField(fieldName, dbType, b);
                }
            }
        }
        return fieldName;
    }
}
